package com.example.demo.service.impl;

import com.example.demo.config.CommonConstants;
import com.example.demo.dao.VideoCardDao;
import com.example.demo.model.VideoCardDO;
import com.example.demo.service.VideoCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 视频卡片信息服务层实现类
 */
@Service
public class VideoCardServiceImpl implements VideoCardService {
    /**
     * 数据库操作层
     */
    @Autowired
    private VideoCardDao videoCardDao;
    /**
     * redis缓存
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询视频卡片信息 缓存
     * @return 返回list
     */
    @Override
    public List<VideoCardDO> findAll() {
        List<VideoCardDO> videoCards = null;
        //从redis中查询
        Object object = redisTemplate.opsForValue().get(CommonConstants.CAPTCHA_VIDEO_CARD);
        //redis中不为空，则赋值
        if(object != null){
            videoCards = (List<VideoCardDO>)object;
        //为空 则从数据库中获取，并存入到redis中
        }else{
            System.out.println("从数据库中获取，存入到redis中");
            videoCards = videoCardDao.getVideoCards();
            if(videoCards != null && !videoCards.isEmpty()){
                redisTemplate.opsForValue().set(CommonConstants.CAPTCHA_VIDEO_CARD,videoCards,300000, TimeUnit.MILLISECONDS);
            }

        }
        return videoCards;
    }

    /**
     * 查询视频卡片信息 无缓存
     * @return 返回列表
     */
    @Override
    public List<VideoCardDO> findAllNoCache() {
        return videoCardDao.getVideoCards();
    }
}
